############## 
############## 
############## Models
remove(list = ls())

base::library(conflicted)
base::library(tidyverse)
conflict_prefer("filter","dplyr")
base::library(ggplot2)
base::library(dplyr)
base::library(here)
conflict_prefer("here", "here")
base::library(systemfit)
base::library(stargazer)
base::library(DataCombine)
base::library(haven)
base::library(nnet)
base::library(ggeffects)

over_data <- read.csv(here("Data", "dcinbox_emails_overdose.csv"))

glimpse(over_data)

#Overdose
#2012-2024
time <- c(rep(1:22, 2))
time
party <- c(rep("dem", 22), rep("rep", 22))
party
dem <- c(0, 5, 4, 4, 15, 3, 13,
         53, 27, 26, 61, 64, 36,
         48, 22, 30, 21, 37, 27,
         43, 87, 58)
dem
rep <- c(5, 8, 6, 13, 18, 22, 52,
         180, 123, 85, 117, 229, 138,
         74, 41, 54, 63, 150, 265,
         195, 194, 114)
rep
emails <- c(dem, rep)
emails

date <- rep(c("12/2011 - 12/2012",
          "12/2013 - 02/2013",
          "02/2013 - 08/2013",
          "08/2013 - 03/2014",
          "03/2014 - 10/2014",
          "10/2014 - 05/2015",
          "05/2015 - 11/2015",
          "11/2015 - 06/2016",
          "06/2016 - 01/2017",
          "01/2017 - 08/2017",
          "08/2017 - 02/2018",
          "02/2018 - 09/2018",
          "09/2018 - 04/2019",
          "04/2019 - 11/2019",
          "11/2019 - 05/2020",
          "05/2020 - 12/2020",
          "12/2020 - 07/2021",
          "07/2021 - 02/2022",
          "02/2022 - 08/2022",
          "08/2022 - 03/2023",
          "03/2023 - 10/2023",
          "10-2023 - 5/2024"), 2)

overdose <- data.frame(cbind(time, date, party, emails))
glimpse(overdose)

overdose <- overdose %>% 
  mutate(emails = as.numeric(emails)) %>% 
  mutate(date = factor(date, levels = c("12/2011 - 12/2012",
                                  "12/2013 - 02/2013",
                                  "02/2013 - 08/2013",
                                  "08/2013 - 03/2014",
                                  "03/2014 - 10/2014",
                                  "10/2014 - 05/2015",
                                  "05/2015 - 11/2015",
                                  "11/2015 - 06/2016",
                                  "06/2016 - 01/2017",
                                  "01/2017 - 08/2017",
                                  "08/2017 - 02/2018",
                                  "02/2018 - 09/2018",
                                  "09/2018 - 04/2019",
                                  "04/2019 - 11/2019",
                                  "11/2019 - 05/2020",
                                  "05/2020 - 12/2020",
                                  "12/2020 - 07/2021",
                                  "07/2021 - 02/2022",
                                  "02/2022 - 08/2022",
                                  "08/2022 - 03/2023",
                                  "03/2023 - 10/2023",
                                  "10-2023 - 5/2024"),
                       labels = c("12/2011 - 12/2012",
                                  "12/2013 - 02/2013",
                                  "02/2013 - 08/2013",
                                  "08/2013 - 03/2014",
                                  "03/2014 - 10/2014",
                                  "10/2014 - 05/2015",
                                  "05/2015 - 11/2015",
                                  "11/2015 - 06/2016",
                                  "06/2016 - 01/2017",
                                  "01/2017 - 08/2017",
                                  "08/2017 - 02/2018",
                                  "02/2018 - 09/2018",
                                  "09/2018 - 04/2019",
                                  "04/2019 - 11/2019",
                                  "11/2019 - 05/2020",
                                  "05/2020 - 12/2020",
                                  "12/2020 - 07/2021",
                                  "07/2021 - 02/2022",
                                  "02/2022 - 08/2022",
                                  "08/2022 - 03/2023",
                                  "03/2023 - 10/2023",
                                  "10-2023 - 5/2024")))

fig1 <- ggplot(overdose, aes(x = date, y = emails, 
                                    color = party, fill = party)) +
  geom_bar(position="dodge", stat="identity") +
  scale_color_manual(name = "Party", 
                     label = c("Dem", "Rep"), 
                     values = c("blue", "red")) +
  scale_fill_manual(name = "Party", 
                    label = c("Dem", "Rep"), 
                    values = c("blue", "red")) +
  theme_bw() + 
  xlab(NULL) +
  ylab("Frequency") +
  ggtitle("A. Mentions of 'Overdose' in Congressional Emails") +
  scale_y_continuous(breaks = c(50, 100, 150, 200, 250)) +
  theme(axis.text.x = element_text(size = 10,
                                   angle = 45, vjust = 1, hjust=1),
        axis.title.x = element_text(size = 13),
        axis.text.y = element_text(size = 11),
        axis.title.y = element_text(size = 13),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 13),
        title = element_text(size = 13))

fig1

#Overdose
#2012-2024
time <- c(rep(1:22, 2))
time
party <- c(rep("dem", 22), rep("rep", 22))
party
dem <- c(0, 2, 4, 4, 14, 20, 18, 26, 19, 12, 20, 12, 1,
         9, 11, 14, 20, 18, 44, 98, 74, 122)
dem
rep <- c(1, 8, 25, 20, 39, 30, 56, 144, 108, 62, 33, 39, 15,
         23, 117, 154, 359, 336, 472, 599, 382, 463)
rep
emails <- c(dem, rep)
emails

date <- rep(c("05/2015 - 10/2015",
              "10/2015 - 03/2016",
              "03/2016 - 07/2016",
              "07/2016 - 12/2016",
              "12/2016 - 05/2017",
              "05/2017 - 10/2017",
              "10/2017 - 03/2018",
              "03/2018 - 08/2018",
              "08/2018 - 01/2019",
              "01/2019 - 06/2019",
              "06/2019 - 11/2019",
              "11/2019 - 04/2020",
              "04/2020 - 09/2020",
              "09/2020 - 01/2021",
              "01/2021 - 06/2021",
              "06/2021 - 11/2021",
              "11/2021 - 04/2022",
              "04/2022 - 09/2022",
              "09/2022 - 02/2023",
              "02/2023 - 07/2023",
              "07/2023 - 12/2023",
              "12-2023 - 5/2024"), 2)

fentanyl <- data.frame(cbind(time, date, party, emails))
glimpse(fentanyl)


fentanyl <- fentanyl %>% 
  mutate(emails = as.numeric(emails)) %>% 
  mutate(date = factor(date, levels = c("05/2015 - 10/2015",
                                        "10/2015 - 03/2016",
                                        "03/2016 - 07/2016",
                                        "07/2016 - 12/2016",
                                        "12/2016 - 05/2017",
                                        "05/2017 - 10/2017",
                                        "10/2017 - 03/2018",
                                        "03/2018 - 08/2018",
                                        "08/2018 - 01/2019",
                                        "01/2019 - 06/2019",
                                        "06/2019 - 11/2019",
                                        "11/2019 - 04/2020",
                                        "04/2020 - 09/2020",
                                        "09/2020 - 01/2021",
                                        "01/2021 - 06/2021",
                                        "06/2021 - 11/2021",
                                        "11/2021 - 04/2022",
                                        "04/2022 - 09/2022",
                                        "09/2022 - 02/2023",
                                        "02/2023 - 07/2023",
                                        "07/2023 - 12/2023",
                                        "12-2023 - 5/2024"),
                       labels = c("05/2015 - 10/2015",
                                  "10/2015 - 03/2016",
                                  "03/2016 - 07/2016",
                                  "07/2016 - 12/2016",
                                  "12/2016 - 05/2017",
                                  "05/2017 - 10/2017",
                                  "10/2017 - 03/2018",
                                  "03/2018 - 08/2018",
                                  "08/2018 - 01/2019",
                                  "01/2019 - 06/2019",
                                  "06/2019 - 11/2019",
                                  "11/2019 - 04/2020",
                                  "04/2020 - 09/2020",
                                  "09/2020 - 01/2021",
                                  "01/2021 - 06/2021",
                                  "06/2021 - 11/2021",
                                  "11/2021 - 04/2022",
                                  "04/2022 - 09/2022",
                                  "09/2022 - 02/2023",
                                  "02/2023 - 07/2023",
                                  "07/2023 - 12/2023",
                                  "12-2023 - 5/2024")))

fig2 <- ggplot(fentanyl, aes(x = date, y = emails, 
                             color = party, fill = party)) +
  geom_bar(position="dodge", stat="identity") +
  scale_color_manual(name = "Party", 
                     label = c("Dem", "Rep"), 
                     values = c("blue", "red")) +
  scale_fill_manual(name = "Party", 
                    label = c("Dem", "Rep"), 
                    values = c("blue", "red")) +
  theme_bw() + 
  xlab(NULL) +
  ylab("Frequency") +
  ggtitle("B. Mentions of 'Fentanyl' in Congressional Emails") +
  scale_y_continuous(breaks = c(100, 200, 300, 400, 500, 600, 700)) +
  theme(axis.text.x = element_text(size = 10,
                                   angle = 45, vjust = 1, hjust=1),
        axis.title.x = element_text(size = 13),
        axis.text.y = element_text(size = 11),
        axis.title.y = element_text(size = 13),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 13),
        title = element_text(size = 13))

fig2


multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  numPlots = length(plots)
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  if (numPlots==1) {
    print(plots[[1]])
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

multiplot(fig1, fig2, cols = 1)




